$(function(){
   $('#goout').on('click',function(){
       layer.confirm('确认要退出吗？', { icon: 3, title: '提示' }, function (index) {
           location.href = '/login.html'
           localStorage.removeItem('token')
  
           layer.close(index);
       })
      
   })

   getUserinfo()
})

function getUserinfo(){
    axios({
        url:'/my/userinfo',
        method:'get',
    }).then(({data:res})=>{
        console.log(res)
        if(res.status!==0){
            return layer.msg(res.message)
        }
        renderAvatar(res.data)
    })
}

function renderAvatar(user){
    let name = user.nickname || user.username
    $('.welcome').html('欢迎您'+name)

    if (user.user_pic!==null){
        $('.layui-nav-img').show().attr('src',user.user_pic)
        $('.avatar').hide()
    }else{
        let first=name[0].toUpperCase()
        $('.layui-nav-img').hide()
        $('.avatar').show().html(first)
    }
}